
//
//  NewLSYBottomViewMenu.h
//  LSYReader
//
//  Created by 王欣 on 2017/12/22.
//  Copyright © 2017年 okwei. All rights reserved.
//




#import "CBCPowerView.h"
#import  "CommonFuctions.h"
@interface CBCPowerView()
@end

@implementation CBCPowerView


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self refeshUI];
        
    }
    
    return self;
}

-(void)refeshUI
{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview ];
    }
   if (_currentNum < 0.2 && _currentNum > 0)
    {
        self.backView = [self creatViewWithImage:@"低电警告.png"];
        [self initfrontViewWithColor:[UIColor colorWithRed:255/255.0 green:154/255.0 blue:0/255.0 alpha:1]];
        
    }else
    {
        self.backView = [self creatViewWithImage:@"电量.png"];
        [self initfrontViewWithColor:[UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1]];
        
    }
    
    [self addSubview:self.backView];

}
-(void)refeshUI2
{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview ];
    }
    self.backView = [self creatViewWithImage:@"充满电.png"];
    [self initfrontViewWithColor:[UIColor colorWithRed:0/255.0 green:186/255.0 blue:53/255.0 alpha:1]];
    
    [self addSubview:self.backView];
}

-(UIView*)creatViewWithImage:(NSString*)imageName
{
    UIView *bgview = [[UIView alloc]initWithFrame:self.bounds];
    bgview.clipsToBounds = YES;
    bgview.backgroundColor = [UIColor clearColor];


    UIImageView *imageView = [[UIImageView alloc]initWithImage:[CommonFuctions cbcReaderImage:imageName]];
    imageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    [bgview addSubview:imageView];

    
    return bgview;
}

-(void)initfrontViewWithColor:(UIColor*)bgcolor
{
    self.frontView = [[UIView alloc]initWithFrame:CGRectMake(2, 2, 14.5, 6)];
    self.frontView.backgroundColor = bgcolor;
    [self.backView addSubview:self.frontView];
}

-(void)layoutSubviews
{
    
    [super layoutSubviews];
    
    if ([_charging isEqualToString:@"1"])
    {
        [self refeshUI2];
    }else
    {
        [self refeshUI];
        
    }
    self.frontView.frame = CGRectMake(2, 2, 14.5 *_currentNum, 6);
    
}

-(void)setCharging:(NSString *)charging{
    _charging = charging;
    if ([_charging isEqualToString:@"1"])
    {
        [self refeshUI2];
    }else
    {
        [self refeshUI];
        
    }
}
-(void)setPowerPercent:(CGFloat)powerPercent
{
    if (_currentNum < 0) {
        _currentNum = 0;
    }else if (_currentNum > 1)
    {
       _currentNum = 1;
    }else
    {
        _currentNum = powerPercent;
    }
    
    [self setNeedsLayout];
}

-(void)setPowerPercent2:(CGFloat)powerPercent
{
   
    _currentNum = powerPercent;
    
    [self setNeedsLayout];
}

@end
